package com.ect.server.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	public Server() {
		try {
			ServerSocket ss = new ServerSocket(10001);
			System.out.println("The server is waiting client input...");
			Socket socket = ss.accept();
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			while (true) {
				String line = in.readLine();
				System.out.println("Recieve client message : IP:"
						+ socket.getInetAddress().toString() + "  Message:"
						+ line);
				if (line.equalsIgnoreCase("quit")
						|| line.equalsIgnoreCase("exit")) {
					in.close();
					out.close();
					socket.close();
					break;
				}
				// print message into client socket
				out.println("Message From Server,your input is:" + line);
				out.flush();
			}
			ss.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Server();
	}

}
